package com.cdut.config;

/**
 * author: xinmu
 * date: 2022/4/20
 * time: 14:53
 */


import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger使用的配置文件
 */
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
    @Bean
    public Docket createRestApi(Environment environment){

        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev1","dev2");

        //获取项目的环境,通过acceptsProfiles判断是否在设定的环境
        boolean flag = environment.acceptsProfiles(profiles);


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组为base
                .groupName("base")
                .enable(flag)
                .select()
                /*
                RequestHandlerSelectors,配置要扫描接口的方式
                basePackage:指定要扫描的包
                any()扫描全部
                none()不扫描
                withClassAnnotation 扫描类上的注解
                */
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //过滤扫描什么路径
                .paths(PathSelectors.any())
                .build();
    }

    //基本信息的配置，信息会在api文档上显示
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("基本接口测试的接口文档")
                .description("相关接口的文档")
                .termsOfServiceUrl("http://localhost:8080/hello")
                .version("1.0")
                .build();
    }
}